<template>
	<view class="content">
	<button @click="stop">停止</button>
	<button @click="fasong()">发送</button>
	</view>
</template>

<script>
	// import {
	// 	connect
	// } from '@/service/getData2';
	import mqtt from 'mqtt'
	// import PahoMQTT from '@/service/paho-mqtt.js'
	// import mqtt from '@/service/paho-mqtt.js'
	// let client = new PahoMQTT.Client("mqtt.syniot.cn", 443, 'wx_' + Math.random().toString(16).substr(2, 8))
	
import { MQTT_SERVICE, MQTT_USERNAME, MQTT_PASSWORD } from '@/service/sysconstant'
var client
const options = {
  connectTimeout: 40000,
  clientId: 'mqtitId-Home',
  username: MQTT_USERNAME,
  password: MQTT_PASSWORD,
  clean: true
}
client = mqtt.connect(MQTT_SERVICE, options);
export default {
  name: 'viewtest',
  data () {
    return {
      msg: ''
    }
  },
  created () {
    this.mqttMSG()
  },
  methods: {
    mqttMSG () {
      // mqtt连接
      client.on('connect', (e) => {
        console.log('连接成功:')
        client.subscribe('wenyue', { qos: 0 }, (error) => {
          if (!error) {
            console.log('订阅成功')
          } else {
            console.log('订阅失败')
          }
        })
      })
	  client.publish('wenyue',"hhahah")
      // 接收消息处理
      client.on('message', (topic, message) => {
		  console.log(message);
        // console.log('收到来自', topic, '的消息', message.toString())
        // this.msg = message.toString()
      })
      // 断开发起重连
      client.on('reconnect', (error) => {
        console.log('正在重连:', error)
      })
      // 链接异常处理
      client.on('error', (error) => {
        console.log('连接失败:', error)
      })
    },
	fasong(){
		 client.publish('wenyue',"hhahah")
	}
  }
}
// 	export default {
// 		data() {
// 			return {
// 				title: 'Hello'
// 			}
// 		},
// 		
// 		onLoad() {
// 			this.ScanAudio();
// 			
// 		},
// 		methods: {
// stop(){
// 	this.stopaudio()
// }
// 		}
// 	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200upx;
		width: 200upx;
		margin-top: 200upx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50upx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36upx;
		color: #8f8f94;
	}
</style>
